package com.sy.structural.proxy.dynamic;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JDKTikTokProxy<T> implements InvocationHandler {

    private T target;

    public JDKTikTokProxy(T target) {
        this.target = target;
    }

    /**
     * ClassLoader loader,
     * Class<?>[] interfaces,
     * InvocationHandler h
     * @param target
     * @param <T>
     * @return
     */
    public static<T> T getProxy(T target) {
        Object o = Proxy.newProxyInstance(
                target.getClass().getClassLoader(),
                target.getClass().getInterfaces(),
                new JDKTikTokProxy(target)
        );

        return (T)o;
    }


    @Override
    public Object invoke(Object proxy,
                         Method method,
                         Object[] args) throws Throwable {
        Object invoke = method.invoke(target, args);
        System.out.println("增强这个方法");
        return invoke;
    }
}
